﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for BarcodeVernieuwen.xaml
    /// </summary>
    public partial class BarcodeVernieuwen : Page
    {
        private materiaal oldMateriaal = null;
        private toebehoren oldToebehoren = null;

        public BarcodeVernieuwen()
        {
            InitializeComponent();

        }

        private void barcodeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string c = barcodeTextBox.Text.ToLower();
            if (oldMateriaal == null && oldToebehoren == null)
            {
                if (c != "")
                {
                    if (c[0] == 'm')
                    {
                        if (c.Length == 5)
                        {
                            var m = Queries.GetMateriaalByScancode(c);
                            if (m != null)
                            {
                                oldMateriaal = m;
                                getNewCode(true);
                            }
                            barcodeTextBox.Text = "";
                        }
                        else if (c.Length > 5)
                        {
                            barcodeTextBox.Text = "";
                            ErrorLabel.Content = "Te lang";
                        }
                    }
                    else if (c[0] == 't')
                    {
                        if (c.Length == 5)
                        {
                            var t = Queries.GetToebehorenByScancode(c);
                            if (t != null)
                            {
                                oldToebehoren = t;
                                getNewCode(false);
                            }
                            barcodeTextBox.Text = "";
                        }
                        else if (c.Length > 5)
                        {
                            barcodeTextBox.Text = "";
                            ErrorLabel.Content = "Te lang";
                        }
                    }
                    else
                    {
                        barcodeTextBox.Text = "";
                        ErrorLabel.Content = "tik een 5digit code beginnend met m of t in";
                    }
                }
            }
            else
            {
                if (c != "")
                {
                    if (c[0] == 'm' && oldMateriaal!=null)
                    {
                        if (c.Length == 5)
                        {
                            if (!Queries.StreepjescodeExists(c))
                            {
                                Queries.SwitchCode(oldMateriaal, c);
                                getOldCode();
                                ErrorLabel.Content = "Succesvolle switch";
                            }
                            else
                            {
                                ErrorLabel.Content = "De nieuwe code bestaat al";
                            }
                            barcodeTextBox.Text = "";
                        }
                        else if (c.Length > 5)
                        {
                            barcodeTextBox.Text = "";
                            ErrorLabel.Content = "Te lang";
                        }
                    }
                    else if (c[0] == 't' && oldToebehoren != null)
                    {
                        if (c.Length == 5)
                        {
                            if (!Queries.StreepjescodeExists(c))
                            {
                                Queries.SwitchCode(oldToebehoren, c);
                                getOldCode();
                                ErrorLabel.Content = "Succesvolle switch";
                            }
                            else
                            {
                                ErrorLabel.Content = "De nieuwe code bestaat al";
                            }
                            barcodeTextBox.Text = "";
                        }
                        else if (c.Length > 5)
                        {
                            barcodeTextBox.Text = "";
                            ErrorLabel.Content = "Te lang";
                        }
                    }
                    else
                    {
                        barcodeTextBox.Text = "";
                        ErrorLabel.Content = "tik een 5digit code beginnend met m of t in";
                    }
                }
            }
        }

        private void getOldCode()
        {
            oldMateriaal = null;
            oldToebehoren = null;
            scanLabelLabel.Content = "Scan oud label";
            scanLabel2Label.Content = "Of type de code in en duw op <Enter> wanneer scannen niet meer lukt.";
        }

        private void getNewCode(bool isMateriaal)
        {
            ErrorLabel.Content = "";
            scanLabelLabel.Content = "Scan nieuw label";
            if(isMateriaal) scanLabel2Label.Content = "Of type de code beginnend met M in en duw op <Enter> ";
            else            scanLabel2Label.Content = "Of type de code beginnend met T in en duw op <Enter> ";
        }

    }
}
